package com.cool.reggie.core.mybatis;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.cool.reggie.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 自动填充字段
 *
 * @Author ifredomvip@gmail.com
 * @Date 2022/7/5 17:07
 * @Version 1.0.0
 * @Description
 **/
@Slf4j
@Component
public class AutoFillField implements MetaObjectHandler {
    private final static String CREATE_DATE = "createTime";
    private final static String CREATOR = "createUser";
    private final static String UPDATE_DATE = "updateTime";
    private final static String UPDATER = "updateUser";
    private final static String DEL_FLAG = "delFlag";

    @Override
    public void insertFill(MetaObject metaObject) {
        log.info("填充");
        Date date = new Date();

        Long id = BaseContext.getCurrentId();
        System.out.println(id);

        //创建者
        strictInsertFill(metaObject, CREATOR, Long.class, new Long(1));
        //创建时间
        strictInsertFill(metaObject, CREATE_DATE, Date.class, date);
        //更新者
        strictInsertFill(metaObject, UPDATER, Long.class, new Long(1));
        //更新时间
        strictInsertFill(metaObject, UPDATE_DATE, Date.class, date);
        //删除标识
        strictInsertFill(metaObject, DEL_FLAG, Integer.class, DelFlagEnum.NORMAL.value());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Long id = BaseContext.getCurrentId();
        System.out.println(id);

        //更新者
        strictInsertFill(metaObject, UPDATER, Long.class, new Long(1));
        //更新时间
        strictInsertFill(metaObject, UPDATE_DATE, Date.class, new Date());
    }
}
